package com.itheima.app.manager;

import com.itheima.app.interceptor.UserHolder;
import com.itheima.domain.db.*;
import com.itheima.service.db.BlackListService;
import com.itheima.service.db.NotificationService;
import com.itheima.service.db.QuestionService;
import com.itheima.service.db.UserInfoService;
import com.itheima.vo.PageBeanVo;
import com.itheima.vo.SettingVo;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

@Component
public class SettingMananger {

    @DubboReference
    QuestionService questionService;

    @DubboReference
    NotificationService notificationService;

    @DubboReference
    BlackListService blackListService;

    @DubboReference
    UserInfoService userInfoService;

    public ResponseEntity findSetting() {
        //1.创建SettingVo对象
        SettingVo vo = new SettingVo();

        //2.设置用户id和用户手机号
        User user = UserHolder.get();
        vo.setId(user.getId());
        vo.setPhone(user.getMobile());

        //3.远程调用questionService查询用户设置的陌生人问题,设置到vo
        Question question = questionService.findByUserId(user.getId());
        if (question!=null) {
            vo.setStrangerQuestion(question.getStrangerQuestion());
        }

        //4.远程调用notifacationService查询用户的通知设置,设置到vo中
        Notification notification = notificationService.findByUserId(user.getId());
        if (notification!=null) {
            vo.setGonggaoNotification(notification.getGonggaoNotification());
            vo.setLikeNotification(notification.getLikeNotification());
            vo.setPinglunNotification(notification.getPinglunNotification());
        }

        //5.返回vo
        return ResponseEntity.ok(vo);
    }

    public ResponseEntity setQuestion(String content) {
        //1.获取当前登陆者
        User user = UserHolder.get();

        //2.通过用户id查询之前设置的问题
        Question question = questionService.findByUserId(user.getId());

        //3.判断之前是否设置
        if (question == null) {
            //3.1若没有设置过,就是保存
            question = new Question();
            question.setStrangerQuestion(content);
            question.setUserId(user.getId());

            questionService.save(question);
        }else{
            //3.2若设置过,就是更新
            question.setStrangerQuestion(content);

            questionService.update(question);
        }
        return ResponseEntity.ok(null);
    }

    public ResponseEntity setNotification(Notification notification) {
        //1.获取当前登陆者
        User user = UserHolder.get();
        
        //2.通过用户id获取之前的通知设置
        Notification old = notificationService.findByUserId(user.getId());

        //3.判断通知设置是否存在
        if (old == null) {
            //3.1若不存在,就是保存操作
            notification.setUserId(user.getId());

            notificationService.save(notification);
        }else {
            //3.2若存在,就是更新操作
            notification.setId(old.getId());
            notification.setUserId(old.getUserId());

            notificationService.update(notification);
        }

        return ResponseEntity.ok(null);
    }

    public ResponseEntity findBlackListByPage(Integer pageNum, Integer pageSize) {
        //1.获取当前登陆者
        User user = UserHolder.get();

        //2.远程调用service分页查询数据(这里面只有黑名单用户id)
        PageBeanVo vo = blackListService.findByPage(pageNum, pageSize, user.getId());

        //3.遍历返回值中集合,获取每个黑名单用户的id,调用UserInfoService查询用户信息,存入一个新集合中
        List<UserInfo> blackList = new ArrayList<>();

        List<BlackList> items = (List<BlackList>) vo.getItems();
        for (BlackList item : items) {
            //黑名单用户id
            Long blackUserId = item.getBlackUserId();

            //调用UserInfoService查询用户信息
            UserInfo userInfo = userInfoService.findByUserId(blackUserId);

            blackList.add(userInfo);
        }

        //4.将新集合赋值给vo对象
        vo.setItems(blackList);

        //5.返回vo
        return ResponseEntity.ok(vo);
    }


    public ResponseEntity deleteBlackList(Long blackId) {
        //1.获取当前登陆者id
        User user = UserHolder.get();

        //2.远程调用service完成删除操作
        blackListService.deleteByUserIdAndBlackId(user.getId(),blackId);

        return ResponseEntity.ok(null);
    }
}
